package com.example.doctorserver.service.impl;

import com.example.doctorserver.dto.OrdersPageResponseDTO;
import com.example.doctorserver.dto.OrdersResponseDTO;
import com.example.doctorserver.dto.SelectForm;
import com.example.doctorserver.mapper.HospitalMapper;
import com.example.doctorserver.mapper.OrdersMapper;
import com.example.doctorserver.mapper.SetmealMapper;
import com.example.doctorserver.mapper.UsersMapper;
import com.example.doctorserver.response.TijianException;
import com.example.doctorserver.response.TijianExceptionEnum;
import com.example.doctorserver.service.OrdersService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public OrdersPageResponseDTO selectOrderByForm(SelectForm selectForm) {

        Integer page=selectForm.getPageNum()==null?1:selectForm.getPageNum();

        Integer pageSize=selectForm.getMaxPageNum()==null?10:selectForm.getMaxPageNum();

        PageHelper.startPage(page,pageSize);

        Page<OrdersResponseDTO> pages=ordersMapper.selectOrderByForm(selectForm);

        List<OrdersResponseDTO> list=pages.getResult();

        long total=pages.getTotal();
        Integer curPageSize=pages.getPageSize();

        return new OrdersPageResponseDTO(list,total,curPageSize);

    }

    @Override
    public int updateState(Integer orderId, Integer state) {

        int f=ordersMapper.updateState(orderId,state);

        if (f>0){
            return 200;
        }else{
            throw new TijianException(TijianExceptionEnum.KDC_ERR_C_PRINCIPAL_UNKNOWN);
        }
    }
}
